<?php

namespace app\common\model\daily;

use think\Model;

/**
 * 会员模型
 */
class UsersScoreLogModel extends Model
{
    protected $connection = 'database';//连接配置

    // 表名
    protected $name = 'user_score_log';

    // 追加属性
    protected $append = [
        'create_date',
    ];

    /**
     * 获取位置文本
     */
    public function getCreateDateAttr($value, $data)
    {
        if(!isset($data['create_time']) || !$data['create_time']){
            return '';
        }
        return date('Y-m-d H:i:s', $data['create_time']);
    }

    /*score:积分；youth_coin：青年币*/
    public static function increaseUserLog($user_id, $num, $cat, $before = 0, $after = 0, $memo = ''){
        $type = 10;
        if($cat == 'youth_coin') $type = 11;
        self::insert([
            'uid' => $user_id,
            'type' => $type,
            'score' => floatval($num),
            'cat' => $cat,
            'before' => floatval($before),
            'after' => floatval($after),
            'memo' => $memo,
            'way' => 1,
            'create_time' => time()
        ]);
    }


    /*score:积分；youth_coin：青年币*/
    public static function decreaseUserLog($user_id, $num, $cat, $before = 0, $after = 0, $memo = ''){
        $type = 10;
        if($cat == 'youth_coin') $type = 11;
        self::insert([
            'uid' => $user_id,
            'type' => $type,
            'score' => floatval($num),
            'cat' => $cat,
            'before' => floatval($before),
            'after' => floatval($after),
            'memo' => $memo,
            'way' => 2,
            'create_time' => time()
        ]);
    }
    public static function getUsersCatWayTotal($user_id, $cat = 'score', $way = 1){
        return (int)self::where('uid', $user_id)->where('cat', $cat)->where('way', $way)->sum('score');
    }

    public static function getUsersCatLists($user_id, $cat = 1){
        $type = 10;
        if($cat == 2) $type = 11;
        $field = 'id,type,score,cat,before,after,create_time,way,memo as title';
        $lists = self::field($field)->where('uid', $user_id)->where('type', $type)
            ->order('id', 'desc')->paginate(10);
        return $lists;
    }
}